package com.xc.ksxt.controller;

import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.BeanAid;
import com.xc.common.tool.Helper;
import com.xc.db.tables.QuestionCopy;
import com.xc.ksxt.entity.QuestionCopyEntity;
import com.xc.ksxt.service.QuestionCopyService;
import com.xc.ksxt.vo.QuestionCopyVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/questionCopy")
public class QuestionCopyController {
    @Autowired
    private QuestionCopyService questionCopyService;
    private QuestionCopy QUESTIONCOPYTB = QuestionCopy.QUESTION_COPY.as("QUESTIONCOPYTB");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<QuestionCopyVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<QuestionCopyVO> ltr = new LayuiTableResponse<>();
        Condition cond = DSL.trueCondition();
        String libId = request.getParameter("libId");
        String name = request.getParameter("name");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(libId)) {
        	cond = cond.and(QUESTIONCOPYTB.LIB_ID.eq( Integer.valueOf(libId) ));
        }
        if (StringUtils.isNotBlank(name)) {
        	cond = cond.and(QUESTIONCOPYTB.TEXT.like("%"+name+"%"));
        }
        Long count = questionCopyService.count(cond);
        ltr.setCount(count);
        PageInfo<QuestionCopyEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = questionCopyService.select(cond, pageInfo,QUESTIONCOPYTB.INDEX_LETTER.asc());
        List<QuestionCopyVO> questionCopyEntityList = list.into(QuestionCopyVO.class);
        for (QuestionCopyVO questionCopyEntity : questionCopyEntityList){
            setTxt(questionCopyEntity);
        }
        ltr.setData(questionCopyEntityList);
        return ltr;
    }

	private void setTxt(QuestionCopyVO questionCopyEntity) {
		
	}

    @RequestMapping({"/getById"})
    public QuestionCopyVO getById(Long id) {
    	Record r = questionCopyService.getRecordById(id);
    	if(r==null) {
    		return null;
    	}
    	QuestionCopyVO vo = r.into(QuestionCopyVO.class);
        setTxt(vo);
        return vo;
    }
    
    @RequestMapping({"/save"})
    public ResponseEntity<QuestionCopyEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
    	ResponseEntity<QuestionCopyEntity> res = new ResponseEntity<>();
    	QuestionCopyEntity questionCopyEntity = Helper.map2Bean(new QuestionCopyEntity(), params);
    	questionCopyService.save(questionCopyEntity);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<QuestionCopyEntity> delete(@RequestParam("ids[]") Long[] ids) {
        LayuiTableResponse<QuestionCopyEntity> ltr = new LayuiTableResponse<>();
        for (Long id : ids) {
        	questionCopyService.deleteById(id);
        }
        return ltr;
    }

}
